package com.zerdoor.util.chatRecord;

import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.zerdoor.model.ChatingRecord;
/**
 * 操作聊天记录工具类
 * @author angus584
 *
 */
public class ChatingRecordOperation {

	/**
	 * 保存聊天记录
	 * @param channelName 频道命名规则必须为((小)userid-(大)userid)
	 * @param chatingRecord 单条聊天记录
	 * @throws Exception
	 */
	public static void saveChatingRecords(String path,String channelName, ChatingRecord chatingRecord)throws Exception{
		
		//Calendar cal=Calendar.getInstance();//使用日历类
		//int year=cal.get(Calendar.YEAR);//得到年
		//int month=cal.get(Calendar.MONTH)+1;//得到月，因为从0开始的，所以要加1
			
//		path += "/chatingRecords/"+channelName+"/";
//		String fileName = channelName+".clog";
//		File file = new File(path + fileName);

        path += "/chatingRecords/"+channelName+"/";

        //根据路径，文件名得到file
		File file = getFileByChannelName(path,channelName);
		
		List<ChatingRecord> list = null;
		
		System.out.println(path );
		if(!file.exists()){
			File file2 = new File(path);
			if(!file2.exists()){
				file2.mkdirs();
			}
			file.createNewFile();
			
			list = new ArrayList<ChatingRecord>();
			
		}else{
			
			list = getChatingRecordList( file);
			
			
		}

		list.add(chatingRecord);
		
		setChatingRecordList(file,list);
		
	}

    /**
     * 查询聊天记录 (根据房间名得到聊天记录实体对象集合)
     * @param path
     * @param channelName
     * @return
     * @throws Exception
     */
    public static List<ChatingRecord> getChatingRecordList(String path,String channelName) throws Exception{

        path += "/chatingRecords/"+channelName+"/";

        File file = getFileByChannelName(path,channelName);


        return getChatingRecordList(file);
    }

    /**
     * 反序列化聊天记录实体集合
     * @param file
     * @return 聊天记录实体集合
     * @throws Exception
     */
	private static List<ChatingRecord> getChatingRecordList(File file) throws Exception{

        List<ChatingRecord> list = null;
        try{
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);

            list = (List<ChatingRecord>)ois.readObject();

        }catch (FileNotFoundException ex){

            //无聊天记录
        }



        return list;
    }

    /**
     * 序列化保存聊天记录实体对象
     * @param file
     * @param list 聊天记录集合
     * @throws Exception
     */
	private static void setChatingRecordList(File file,List<ChatingRecord> list) throws Exception{
		
		FileOutputStream out;
		
		out = new FileOutputStream(file);
		ObjectOutputStream objOut = new ObjectOutputStream(out);
		objOut.writeObject(list);
		objOut.flush();
		objOut.close();
		
	}

    /**
     * 根据房间名得到file实体
     * @param path
     * @param channelName
     * @return
     * @throws Exception
     */
    private static File getFileByChannelName(String path,String channelName) throws Exception{


        String fileName = channelName+".clog";
        File file = new File(path + fileName);


        return file;
    }
	
	
}
